import { JsonPipe } from '@angular/common';
import {
  ChangeDetectionStrategy,
  Component,
  Inject,
  Signal,
} from '@angular/core';
import { AbsInfoService, Info } from '../interface';

@Component({
  selector: 'app-demo1',
  standalone: true,
  imports: [JsonPipe],
  templateUrl: './demo1.component.html',
  styleUrl: './demo1.component.css',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class Demo1Component {
  successMessages: Signal<Info[]>;
  errorMessages: Signal<Info[]>;

  constructor(
    @Inject(AbsInfoService) private allInfoServices: AbsInfoService[]
  ) {
    this.successMessages = allInfoServices[0].all;
    this.errorMessages = allInfoServices[1].all;
  }

  addSuccessInfo(message: string) {
    this.allInfoServices.forEach((i) => i.record({ message, type: 'success' }));
  }

  addErrorInfo(message: string) {
    this.allInfoServices.forEach((i) => i.record({ message, type: 'error' }));
  }
}
